package com.aquarium.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.aquarium.R.string;
//import com.aquarium.history.HistoryData;
import com.aquarium.sensor.SensorManager;
import com.aquarium.util.SensorData;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class HistoryDataBase extends SQLiteOpenHelper {
	public static final String TAG = "HistoryDataBase";
	public static final String HISTORY_TAB = "history_tab";
	public static final String CONFIG_TAB = "config_tab";
	private static final int VERSION = 1;
	public static final String ID = "_id";
	public static final String KEY = "key";
	public static final String VALUE = "value";
	public static final String TIME = "time";
	public static final String PH = "ph";
	public static final String TEMPERATURE = "temperature";
	public static final String CONDUCTIVITY = "conductivity";
	public static final String OXYGEN = "oxygen";
	public static final String WATER_LEVEL = "water_level";
	public static final String STORE_SENSOR_VALUE_TIME = "srore_sensor_value_time";
	static HistoryDataBase historyDataBase;
	SQLiteDatabase sqLiteDatabase;

	private HistoryDataBase(Context context) {
		super(context, HISTORY_TAB, null, VERSION);
		sqLiteDatabase = getWritableDatabase();
	}

	public static HistoryDataBase getInstance(Context context) {
		if (historyDataBase == null) {
			historyDataBase = new HistoryDataBase(context);
		}
		return historyDataBase;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS " + HISTORY_TAB + " (" + ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TIME + " INTEGER, "
				+ PH + " INTEGER, " + TEMPERATURE + " INTEGER, " + CONDUCTIVITY
				+ " INTEGER, " + OXYGEN + " INTEGER, " + WATER_LEVEL
				+ " INTEGER" + ");");
		db.execSQL("CREATE TABLE IF NOT EXISTS " + CONFIG_TAB + " (" + ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY + " TEXT, "
				+ VALUE + " TEXT " + ");");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onCreate(db);

	}

	boolean insertKeyValue(String key, String value) {
		ContentValues values = new ContentValues();
		values.put(VALUE, value);
		values.put(KEY, key);
		long i = sqLiteDatabase.insert(CONFIG_TAB, null, values);
		if (i != -1) {
			return true;
		}
		return false;

	}

	public boolean updateKeyValue(String key, String value) {
		ContentValues values = new ContentValues();
		values.put(VALUE, value);
		int i = sqLiteDatabase.update(CONFIG_TAB, values, KEY + " = ? ",
				new String[] { key });
		if (i == 1) {
			return true;
		}
		if (i > 1) {
			Log.e(TAG, "");
			return false;
		}
		if (i == 0) {
			return insertKeyValue(key, value);
		}
		return false;
	}

	public String queryKeyValue(String key) {
		Cursor cursor = sqLiteDatabase.query(CONFIG_TAB,
				new String[] { VALUE }, KEY + " = ? ", new String[] { key },
				null, null, null);
		if (cursor == null) {
			Log.e(TAG, "cursor==null");
			return null;
		}
		// cursor.moveToFirst();
		while (cursor.moveToNext()) {
			int i = cursor.getColumnIndex(VALUE);
			String string = cursor.getString(i);
			cursor.close();
			return string;
		}
		return null;
	}

	public void insertHistoryItem(String time, HashMap<String, String> data) {
		float mCurrentPhValue = Float.parseFloat(data
				.get(SensorManager.PH_VALUE_KEY));
		float mCurrentTemperatureValue = Float.parseFloat(data
				.get(SensorManager.TEMPERATURE_VALUE_KEY));
		float mCurrentOxygenValue = Float.parseFloat(data
				.get(SensorManager.OXYGEN_VALUE_KEY));
		float mCurrentConductivityValue = Float.parseFloat(data
				.get(SensorManager.CONDUCTIVITY_VALUE_KEY));
		float mCurrentWaterLevelValue = Float.valueOf(data
				.get(SensorManager.WATER_LEVEL_VALUE_KEY));
		ContentValues values = new ContentValues();
		values.put(TIME, time);
		values.put(PH, mCurrentPhValue);
		values.put(TEMPERATURE, mCurrentTemperatureValue);
		values.put(CONDUCTIVITY, mCurrentConductivityValue);
		values.put(OXYGEN, mCurrentOxygenValue);
		values.put(WATER_LEVEL, mCurrentWaterLevelValue);
		Log.d(TAG, time + mCurrentConductivityValue + mCurrentOxygenValue);
		sqLiteDatabase.insert(HISTORY_TAB, null, values);
	}

	public List<SensorData> queryItems(String limit) {
		List<SensorData> list = new ArrayList<SensorData>();
//		String sql="select * from "+HISTORY_TAB+" order by "+ID +" desc "+" limit "+limit+";";
		Cursor cursor = sqLiteDatabase.query(HISTORY_TAB, new String[] { ID,
				TIME, PH, TEMPERATURE, CONDUCTIVITY, OXYGEN, WATER_LEVEL },
				null, null, null, null, ID+" desc", limit);
		if (cursor == null || !cursor.moveToFirst())
			return list;
	
		do {
			SensorData mSensorData = new SensorData();
			mSensorData.time = (cursor.getString(cursor.getColumnIndex(TIME)));
			mSensorData.ph = (cursor.getFloat(cursor.getColumnIndex(PH)));
			mSensorData.temperature = (cursor.getFloat(cursor
					.getColumnIndex(TEMPERATURE)));
			mSensorData.conductivity = (cursor.getFloat(cursor
					.getColumnIndex(CONDUCTIVITY)));
			mSensorData.oxygen = (cursor
					.getFloat(cursor.getColumnIndex(OXYGEN)));
			mSensorData.water_level = (cursor.getFloat(cursor
					.getColumnIndex(WATER_LEVEL)));
			list.add(mSensorData);
		} while (cursor.moveToNext());
		cursor.close();
		Log.d(TAG, "list size" + list.size());
		return list;
	}
}
